//聊天列表展示
function showChat(wxUserId) {
    $.ajax({
        url : "chat/list",
        async : true,
        type : "GET",
        data : {
            "wxUserId" : wxUserId
        },
        // 成功后开启模态框
        success : queryEdit,
        error : function(res) {
            alert("连接失败");
        },
        dataType : "json"
    });
}
function queryEdit(res) {
    if(res.code!=0){
        alert(res.msg);
    }
    var list = res.data.list;
    var chatHtml = "";
    for (var i = 0; i < list.length; i++) {
        var model = list[i];
        if(model.chatRole==0) {
            chatHtml += '<div class="form-group">' +
                '<label class="col-sm-3 control-label" style="color: #2b669a">'+ model.nickname +'：</label>' +
                '<div class="col-sm-9">' +
                '<p class="form-control-static">'+ model.chatText +'</p>' +
                '</div>' +
                '</div>';
        }else if(model.chatRole==1) {
            chatHtml += '<div class="form-group">' +
                '<label class="col-sm-3 control-label">客服：</label>' +
                '<div class="col-sm-9">' +
                '<p class="form-control-static">'+ model.chatText +'</p>' +
                '</div>' +
                '</div>';
        }
    }
    $("#e_wxUserId").val(list[0].wxUserId);
    $("#chat-table").html(chatHtml);
    // 显示模态框
    $('#editModal').modal('show');
}


//商品添加提交
$("#editButton").click(function () {
    var data = new FormData(document.getElementById("editForm"));
    //判断是否为空
    if(data.get('chatText')===null||data.get('chatText')===undefined||data.get('chatText')==''||
        data.get('wxUserId')===null||data.get('wxUserId')===undefined||data.get('wxUserId')==''){
        alert("内容不能为空，请检查。");
        return 0;
    }
    $.ajax({
        type: 'post',
        url: 'chat/send',
        dataType: 'json',
        data: data,
        contentType: false, //不设置内容类型
        processData: false, //不处理数据
        error: function(request) {
            alert("未知错误，请检查输入格式！");
        },
        success: function(res) {
            if(res.code==0) {
                showChat(data.get('wxUserId'));
                $("#e_chatText").val('');
            }else{
                alert(res.msg);
            }
        }
    });
});
